{% extends 'df_cart/base.html' %}
{% load cartSum %}

    {% block body %}
	<div class="total_count">全部商品<em class="cartCount">{{ cartCount }}</em>件</div>
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
    {% for foo in goodsList %}
        <ul class="cart_list_td clearfix" >
            <li class="col01"><input onclick="getCheckBox();" type="checkbox" i="{{ foo.id }}" name="" checked></li>
            <li class="col02"><img src="/{{ foo.goods.cover }}"></li>
            <li class="col03">{{ foo.goods.title }}<br><em>{{ foo.goods.unit }}</em></li>
            <li class="col04">{{ foo.goods.unit }}</li>
            <li class="col05">{{ foo.goods.price }}</li>
            <li class="col06">
                <div class="num_add" i="{{ foo.id }}">
                    <a href="javascript:" class="add fl">+</a>
                    <input type="text" class="num_show fl" value="{{ foo.amount }}">
                    <a href="javascript:" class="minus fl">-</a>
                </div>
            </li>
            <li class="col07">{% subtotal foo.goods.price foo.amount %}</li>
            <li class="col08" onclick="delCartGoods(this);"><a href="javascript:;">删除</a></li>
        </ul>
    {% endfor %}
	

	<ul class="settlements" >
		<li class="col01"><input id="checked_all" type="checkbox" name="" checked="checked" ></li>
		<li class="col02">全选</li>
		<li class="col03" >合计(不含运费)：<span>¥</span><em id="Account">{{ account }}</em><br>共计<b class="cartCount" id="amount">{{ cartCount }}</b>件商品</li>
		<li class="col04"><a onclick="settleAccounts();">去结算</a></li>
	</ul>

    {% endblock body %}


    {% block script %}
        <script>
        function checkAmount(){
            console.log($('#amount').text());
            if ($('#amount').text()=="0"){
                console.log('sdf');
                var settlements=$('.settlements').hide();
            }
        }
        checkAmount();
        $('.num_add .minus').click(function () {
          var amount=Number($(this).prev().val());
          if(amount>1){
                amount=--amount;
                $(this).prev().val(amount);
            }
          sumPrice(this,amount);
          console.log(this);
          changeAccount(this);
        });
        $('.num_add .add').click(function () {
          var amount=Number($(this).next().val());
          amount=++amount;
          $(this).next().val(amount);
          sumPrice(this,amount);
          changeAccount(this);
        });

        $('.num_add .num_show').blur(function () {
            var count=$(this).val();
            if (!count){
                $(this).val('1');
            }
            if (count==0){
                $(this).val('1');
            }
            sumPrice(this,count);
            changeAccount(this);
        });

        function sumPrice(self,count){
            if(count==0){
                count=1;
            }
            var price=$(self).parent().parent().prev().text();
            $(self).parent().parent().next().text(price*count);
        }
        function changeAccount(self) {
            var count=0;
            if ($(self).text()=="+"){
                count=Number($(self).next().val());
            }else if($(self).text()=="-"){
                count=Number($(self).prev().val());
            }else{
                count=Number($(self).val());
            }
            console.log('物品数量：',count);
            $.ajax({
                url:'/cart/changeAccount',
                type:'get',
                headers: {'X-CSRFtoken': $.cookie('csrftoken')},
                data:{'id':$(self).parent().attr('i'),'count':count},
                success:function (arg) {
                    $('#Account').text(arg);
                }
            })
        }
        function delCartGoods(self) {
            var gid=$(self).prev().prev().children().attr('i');
            $.ajax({
                url:'/cart/delCartGoods/',
                type:'post',
                headers: {'X-CSRFtoken': $.cookie('csrftoken')},
                data:{'gid':gid},
                success:function (arg) {
                    ret=JSON.parse(arg);
                    if(ret["status"]){
                        $(self).parent().remove();
                        var count=ret["data"];
                        $('.cartCount').text(count);
                        checkAmount();
                    }
                    else{
                        alert('删除失败');
                    }

                }
            });

        }
        $('#checked_all').change(function () {
            //全选和取消
            $(':checkbox:not(#checked_all)').prop('checked',$(this).prop('checked')?true:false);
            getCheckBox();//根据选择更新价格
        });

        function getChecked(state=false){
            var list=[];
            checkBoxAll=$('input:checkbox').each(function () {
                var checked=$(this).prop('checked');
                if(state){//获取所有“被选中”的购物车id
                    if (checked){
                        list.push($(this).attr('i'))
                    }
                }else{//获取所有“没被选中”的购物车id
                    if (!checked){
                        list.push($(this).attr('i'))
                    }
                }
            });
            return list
        }

        //用于更新价格
        function getCheckBox() {
            var list=getChecked();
            $.ajax({
                url:'/cart/sumCart/',
                type:'get',
                headers: {'X-CSRFtoken': $.cookie('csrftoken')},
                data:{'list':list},
                success:function (arg) {
                    $('#Account').text(arg);
                }
            })
        }
        function settleAccounts(){
            var list=getChecked(true);//获取所有被选中的购物车id
            var amount=$('#Account').text();//共计多少钱
            var account=$('#amount').text();//共计几件商品
            $.ajax({
                url:'/order/order/',
                type:'post',
                data:{'list':list,'amount':amount,'account':account},
                headers: {'X-CSRFtoken': $.cookie('csrftoken')},
                success:function (arg) {
                    console.log(list);
                    ret=JSON.parse(arg);
                    console.log(ret["status"]);
                    if(ret["status"]){
                        window.location.href="/order/order/";
                    }
                }
            })

        }
    </script>
    {% endblock script %}